// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   ChoiceElement.java

package app;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import ui.*;

public class ChoiceElement extends Element
{

    public ChoiceElement(String label, String choices[], int left, int top, int width)
    {
        super(left, top);
        super.eventable = true;
        super.selectable = true;
        choice = -1;
        selected = 0;
        super.width = width;
        
        labels = Utils.getArrayFitString(BrowserCanvas.FONTSMALL, label, width);
        super.height = BrowserCanvas.FONTSMALL.getHeight() * labels.length;
        options = new Object[choices.length];
        for(int i = 0; i < choices.length; i++)
        {
            String opt[] = Utils.getArrayFitString(BrowserCanvas.FONTSMALL, choices[i], width - 10);
            super.height += BrowserCanvas.FONTSMALL.getHeight() * opt.length;
            options[i] = opt;
        }

    }

    public void draw(Graphics g, int x, int y, int startY, int stopY, boolean selected)
    {
        y += super.top;
        if(selected)
            g.drawRect(x, y, super.width, super.height);
        g.setColor(0);
        g.setFont(BrowserCanvas.FONTSMALL);
        int i;
        for(i = 0; i < labels.length && y < startY; i++)
            y += BrowserCanvas.FONTSMALL.getHeight();

        if(i > 0)
        {
            i--;
            y -= BrowserCanvas.FONTSMALL.getHeight();
        }
        for(; i < labels.length && y < stopY; i++)
        {
            g.drawString(labels[i], x, y, 20);
            y += BrowserCanvas.FONTSMALL.getHeight();
        }

        if(y >= stopY)
            return;
        for(i = 0; i < options.length && y < stopY; i++)
        {
            String opt[] = (String[])(String[])options[i];
            g.setColor(0xf26522);
            g.fillArc(x, y, 10, 10, 0, 360);
            g.setColor(0);
            if(choice == i)
                g.fillArc(x + 2, y + 2, 6, 6, 0, 360);
            if(selected && this.selected == i)
                g.drawRect(x, y, super.width, BrowserCanvas.FONTSMALL.getHeight() * opt.length);
            for(int j = 0; j < opt.length && y < stopY; j++)
            {
                g.drawString(opt[j], x + 10, y, 20);
                y += BrowserCanvas.FONTSMALL.getHeight();
            }

        }

    }

    public int getChoice()
    {
        return choice;
    }

    public boolean keyPress(int keycode)
    {
        int no = 0;
        if(BrowserCanvas.KEYCENTER == keycode)
            if(choice != selected)
            {
                choice = selected;
                return true;
            } else
            {
                return false;
            }
        if(BrowserCanvas.KEYDOWN == keycode)
        {
            if(selected < options.length)
            {
                selected++;
                return true;
            }
        } else
        if(BrowserCanvas.KEYUP == keycode && selected > 0)
        {
            selected--;
            return true;
        }
        return super.keyPress(keycode);
    }

    private int choice;
    private int selected;
    private String labels[];
    private Object options[];
}
